package UILayer;
import java.util.Iterator;
import CtrLayer.OrderCtr;
import ModelLayer.Order;
import ModelLayer.LineItem;
import ModelLayer.Product;

public class OrderUI extends InOut
{

    private static int idO = 1000;
    private static int saleID = 1000;
    private static OrderCtr orderCtr = new OrderCtr();

    public OrderUI()
    {

    }

    public void orderMenu()
    {
        String[] orderMenu = {"Order & Leasing MENU", "Create order", "Add product","Remove product", "Payment", "Read order", "Change state", "Delivery discount", "Quantity discount"};
        printMenu(orderMenu);
    }

    public void start()
    {
        boolean exit = false;
        while(!exit)
        {
            erase(1);
            orderMenu();
            int choice = inputInt("Choose: ");
            erase(1);
            switch(choice)
            {
                case 1 : { createOrder();  }break;
                case 2 : { addProduct();   }break;
                case 3 : { addProduct();   }break;
                case 4 : { payment();      }break;
                case 5 : { readOrder();    }break;
                case 6 : { changeState();  }break;
                case 7 : { deliveryDiscount();  }break;
                case 8 : { quantityDiscount();  }break;
                case 9 : { exit=true;}break;
                
                default : {}; break;
            }
            if(choice>=1 && choice<=8) waitK();
        }
    }
    
    public void removeProduct()
    {
        int idO1 = inputInt("Order ID (this order's id="+idO+"): ");
        int idP = inputInt("Product ID: ");
        if(orderCtr.removeProduct(idO1, idP)) print(" -> The product was successfully deleted.");
        else print(" -> This order does not exist.");
    }
    
    public void deliveryDiscount()
    {
        int idO1 = inputInt("Order ID (this order's id="+idO+"): ");
        String apply = inputString("Apply delivery discount ? (y/n): ");
        if(orderCtr.deliveryDiscount(idO1,apply)) print(" -> Discount was successfully apllied");
        else print(" -> This order does not exist.");
    }
    
    public void quantityDiscount()
    {
        int idO1 = inputInt("Order ID (this order's id="+idO+"): ");
        double disc = inputDouble("Write discount in percents (e.g. 18%): ");
        if(orderCtr.quantityDiscount(idO1,disc)) print(" -> Discount was successfully apllied");
        else print(" -> This order does not exist.");
    }
    
    public void changeState()
    {
        int idO1 = inputInt("Order ID (this order's id="+idO+"): ");
        String state = inputString("New state (pending, cancel, success): ");
        if(orderCtr.changeState(idO1, state)) print(" -> The state was successfully changed.");
        else print(" -> The order with id "+idO1+" does not exist.");
    }

    public void createOrder() 
    {
        idO++;
        int idC = inputInt("Customer ID: ");
        
        if(orderCtr.createOrder(idO, idC, logUs)) print(" -> A new order was created with id: "+idO);
        else print(" -> The customer is not registered.");
    }

    public void addProduct() 
    {
        int idO1 = inputInt("Order ID (this order's id="+idO+"): ");
        int idP = inputInt("Product ID: ");
        int quantity = inputInt("Quantity: ");
        if(orderCtr.addProduct(idO1, idP, quantity)) print("The product with id "+idP+" was added to order "+idO1);
        else print("The product with id "+idP+" does not exist.");
    }

    public void changeState(int idO, String state) 
    {
        String newValue = inputString(state);
        orderCtr.changeState(idO, state);
    }
    
    public void paymentMenu()
    {
        String[] orderMenu = {"Payment MENU", "Cash", "Credit"};
        printMenu(orderMenu);
    }
    
    public void payment()
    {
        int idO1 = inputInt("Order ID (this order's id="+idO+"): ");
        Order order = orderCtr.getOrder(idO1);
        try{
            double total = order.getFinalTotal();
            double paid = order.getPaidAmount();
            while(paid<=total || paid!=-1)
            {
                paymentMenu();
                int choice = inputInt("Choose: ");
                double amount = inputDouble("Amount: ");
                saleID++;
               
                if( (choice==1 || choice==2))
                {
                   if(orderCtr.addPayment(order, choice, amount, saleID) ) paid += amount;
                   else print(" -> This payment failed. Try another one.");
                }
                else if(choice==3) paid = -1;
            }
            
            print(" -> Return change: "+order.getChange()); 
        }
        catch(Exception e) {print(" -> The order with id "+idO1+" does not exist.");}
    }
    
    public void readOrder() 
    {
        int idOrd = inputInt("Order ID: ");
        Order ord = orderCtr.getOrder(idOrd);
        
        if(ord==null)
        {
            print(" -> That order does not exist");
        }
        else
        {
            ord.updateTotal();
            Iterator it = ord.getProducts();
            
            print("######################");
            print("ID: "+ord.getId());
            print("Employee create order: "+ord.getStaffName());
            print("Employee payment: "+ord.getPaymentStaffName());
            print("Customer: "+ord.getCustomerName());
            print(" --- Products ---");
            while(it.hasNext())
            {
                LineItem li = (LineItem)it.next();
                print(" "+li.getProduct().getName()+" x "+li.getQuantity()+" = "+li.getSubtotal());
            }
            print("-------------------------------");
            print("Subtotal: "+ord.getTotal());
            print("Discount: "+ord.getDiscount());
            print("-------------------------------");
            print("Total: "+ord.getFinalTotal()+" DKK");
            print("State: "+ord.getState());
            print("Start date: "+ord.getStartDate());
            print("End date: "+ord.getEndDate());
        }
    }
}
